package org.jarraiders.musiclibrary.server.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.jarraiders.musiclibrary.domain.Track;
import org.jarraiders.musiclibrary.server.utils.HibernateUtil;

/**
 * @author AlteroStudio, emoagainst
 */
//@Service("trackService")
//@Transactional
public class TrackServiceDAO extends EntityDAOImpl<Track> {
	
	public TrackServiceDAO() {
		super(Track.class);
		setLoggerTarget(TrackServiceDAO.class.getName());
	}
	
	
	@Override
	public List<Track> searchEntity(String searchText){
		logMessage(DEBUG_MSG, "Searching for Entity");
		
		List<Track> result=new ArrayList<Track>();
		
		Session session = null;
		Transaction tx = null;
		
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			Criteria c = session.createCriteria(Track.class);
			c.add(Expression.like("trackTitle", searchText));
			result =c.list();
			
			tx.commit();

			

		} catch (RuntimeException e) {
			if (tx != null && tx.isActive()) {
				try {
					tx.rollback();
				} catch (HibernateException he) {
					logMessage(ERROR_MSG,
							he.getMessage() + ":\n" + e.toString());
					throw new HibernateException(he);
				}
			}
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}

		return result;		
	}

	
}
